package ch.drshit.model.picture;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import ch.drshit.model.utils.BufferedImageHelper;

/**
 * 
 * @author buehlmanntim
 * 
 */
public class Version {
	private int[][] model;

	public Version(BufferedImage pic) {
		int[][] tmpModel = BufferedImageHelper.convertTo2D(pic);
		model = new int[tmpModel[0].length][tmpModel.length];
		for(int i = 0; i < tmpModel.length; i++) {
			for(int r = 0; r < tmpModel[0].length; r++) {
				model[r][i] = tmpModel[i][r];
			}
		}
	}

	public Version(Version version) {
		model = version.getRGBArray();
	}

	// Getters
	
	public BufferedImage getBufferedImage() {
		BufferedImage bI = new BufferedImage(model.length, model[0].length, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < model.length; x++) {
			for (int y = 0; y < model[0].length; y++) {
				bI.setRGB(x, y, model[x][y]);
			}
		}
		return bI;
	}

	public int getRGB(int x, int y) {
		return model[x][y];
	}

	public Color getColor(int x, int y) {
		return new Color(model[x][y]);
	}

	public int[][] getRGBArray() {
		return model;
	}

	public Color[][] getColorArray() {
		int w = this.getWidth();
		int h = this.getHeight();
		Color[][] colors = new Color[w][h];
		for (int x = 0; x < w; x++) {
			for (int y = 0; y < h; y++) {
				colors[x][y] = new Color(model[x][y]);
			}
		}
		return colors;
	}

	public int getHeight() {
		return model[0].length;
	}

	public int getWidth() {
		return model.length;
	}

	// Setters

	public void setArray(int[][] rgbArray) {
		model = rgbArray;
	}

	public void setArray(Color[][] colorArray) {
		int h = this.getHeight();
		int w = this.getWidth();
		int[][] colors = new int[w][h];
		for (int x = 0; x < w; x++) {
			for (int y = 0; y < h; y++) {
				colors[x][y] = colorArray[x][y].getRGB();
			}
		}
		this.model = colors;
	}

	public void setRGB(int x, int y, int rgb) {
		model[x][y] = rgb;
	}

	public void setColor(int x, int y, Color c) {
		model[x][y] = c.getRGB();
	}
}